package com.oncallmng.api;

import com.oncallmng.common.PageParam;
import com.oncallmng.common.PageResult;
import com.oncallmng.common.Result;
import com.oncallmng.model.Schedule;
import com.oncallmng.model.User;
import com.oncallmng.service.ScheduleService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 排班控制器
 */
@RestController
@RequestMapping("/schedules")
@RequiredArgsConstructor
public class ScheduleController {
    
    private final ScheduleService scheduleService;
    
    /**
     * 根据ID查询排班
     * @param id 排班ID
     * @return 排班信息
     */
    @GetMapping("/{id}")
    public Result<Schedule> getById(@PathVariable String id) {
        return Result.success(scheduleService.getById(id));
    }
    
    /**
     * 分页查询排班列表
     * @param pageParam 分页参数
     * @param teamId 团队ID
     * @return 排班列表
     */
    @GetMapping
    public Result<PageResult<Schedule>> page(PageParam pageParam, @RequestParam String teamId) {
        return Result.success(scheduleService.page(pageParam, teamId));
    }
    
    /**
     * 创建排班
     * @param schedule 排班信息
     * @return 排班ID
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> create(@RequestBody Schedule schedule) {
        return Result.success(scheduleService.create(schedule));
    }
    
    /**
     * 更新排班
     * @param id 排班ID
     * @param schedule 排班信息
     * @return 结果
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> update(@PathVariable String id, @RequestBody Schedule schedule) {
        schedule.setId(id);
        scheduleService.update(schedule);
        return Result.success(null);
    }
    
    /**
     * 删除排班
     * @param id 排班ID
     * @return 结果
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> delete(@PathVariable String id) {
        scheduleService.delete(id);
        return Result.success(null);
    }
    
    /**
     * 添加排班成员
     * @param id 排班ID
     * @param userId 用户ID
     * @return 结果
     */
    @PostMapping("/{id}/members/{userId}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> addMember(@PathVariable String id, @PathVariable String userId) {
        scheduleService.addMember(id, userId);
        return Result.success(null);
    }
    
    /**
     * 移除排班成员
     * @param id 排班ID
     * @param userId 用户ID
     * @return 结果
     */
    @DeleteMapping("/{id}/members/{userId}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> removeMember(@PathVariable String id, @PathVariable String userId) {
        scheduleService.removeMember(id, userId);
        return Result.success(null);
    }
    
    /**
     * 调整排班成员顺序
     * @param id 排班ID
     * @param userIds 用户ID列表
     * @return 结果
     */
    @PutMapping("/{id}/members/order")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> adjustMemberOrder(@PathVariable String id, @RequestBody List<String> userIds) {
        scheduleService.adjustMemberOrder(id, userIds);
        return Result.success(null);
    }
    
    /**
     * 获取指定时间的值班人员
     * @param teamId 团队ID
     * @param time 时间点
     * @return 值班人员
     */
    @GetMapping("/oncall")
    public Result<User> getOnCallUser(@RequestParam String teamId, @RequestParam(required = false) LocalDateTime time) {
        if (time == null) {
            time = LocalDateTime.now();
        }
        return Result.success(scheduleService.getOnCallUser(teamId, time));
    }
    
    /**
     * 获取指定日期范围的值班表
     * @param teamId 团队ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 值班表
     */
    @GetMapping("/calendar")
    public Result<List<Schedule>> getOnCallCalendar(
            @RequestParam String teamId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        return Result.success(scheduleService.getOnCallCalendar(teamId, startDate, endDate));
    }
    
    /**
     * 导出排班日历（ICS格式）
     * @param id 排班ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return ICS文件内容
     */
    @GetMapping("/{id}/calendar/export")
    public String exportCalendar(
            @PathVariable String id,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        return scheduleService.exportCalendar(id, startDate, endDate);
    }
} 